<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>nrf51/Source/ext_sensors/cherry8x16/cherry8x16.c Source File</title>
<link href="tabs.css" rel="stylesheet" type="text/css" />
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="alias.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="alias.js"></script>

<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>


<script type="text/javascript">
  jQuery(document).ready(function () {
    if(gref){ // Number all _img and _table classes
      gref();
    }
  });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->

<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">nRF51 General Code Examples
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="annotated.html"><span>Data&#160;Structures</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>Globals</span></a></li>
    </ul>
  </div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('cherry8x16_8c.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">nrf51/Source/ext_sensors/cherry8x16/cherry8x16.c</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.</span>
<a name="l00002"></a>00002 <span class="comment"> *</span>
<a name="l00003"></a>00003 <span class="comment"> * The information contained herein is confidential property of Nordic</span>
<a name="l00004"></a>00004 <span class="comment"> * Semiconductor ASA.Terms and conditions of usage are described in detail</span>
<a name="l00005"></a>00005 <span class="comment"> * in NORDIC SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.</span>
<a name="l00006"></a>00006 <span class="comment"> *</span>
<a name="l00007"></a>00007 <span class="comment"> * Licensees are granted free, non-transferable use of the information. NO</span>
<a name="l00008"></a>00008 <span class="comment"> * WARRANTY of ANY KIND is provided. This heading must NOT be removed from</span>
<a name="l00009"></a>00009 <span class="comment"> * the file.</span>
<a name="l00010"></a>00010 <span class="comment"> *</span>
<a name="l00011"></a>00011 <span class="comment"> */</span>
<a name="l00012"></a>00012 
<a name="l00013"></a>00013 
<a name="l00014"></a>00014 <span class="preprocessor">#include &lt;stdint.h&gt;</span>
<a name="l00015"></a>00015 <span class="preprocessor">#include &lt;stdbool.h&gt;</span>
<a name="l00016"></a>00016 
<a name="l00017"></a>00017 <span class="preprocessor">#include &quot;<a class="code" href="cherry8x16_8h.html" title="Cherry 8x16 keyboard matrix driver.">cherry8x16.h</a>&quot;</span>
<a name="l00018"></a>00018 <span class="preprocessor">#include &quot;nrf.h&quot;</span>
<a name="l00019"></a>00019 
<a name="l00020"></a>00020 <span class="preprocessor">#define CHERRY8x16_NUM_OF_COLUMNS 16 //!&lt; Number of columns in the keyboard matrix</span>
<a name="l00021"></a>00021 <span class="preprocessor"></span><span class="preprocessor">#define CHERRY8x16_NUM_OF_ROWS 8 //!&lt; Number of rows in the keyboard matrix</span>
<a name="l00022"></a>00022 <span class="preprocessor"></span>
<a name="l00023"></a>00023 <span class="preprocessor">#define MODIFIER_HID_START 0xE0</span>
<a name="l00024"></a>00024 <span class="preprocessor"></span><span class="preprocessor">#define MODIFIER_HID_END   0xE7</span>
<a name="l00025"></a>00025 <span class="preprocessor"></span><span class="keyword">static</span> uint8_t m_currently_pressed_keys[<a class="code" href="group__nrf__drivers__cherry8x16.html#ga96705cbd765dd973315e95ac650c68e8" title="Maximum number of pressed keys kept in buffers.">CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS</a>]; 
<a name="l00026"></a>00026 <span class="keyword">static</span> uint8_t m_transmitted_keys[<a class="code" href="group__nrf__drivers__cherry8x16.html#ga96705cbd765dd973315e95ac650c68e8" title="Maximum number of pressed keys kept in buffers.">CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS</a>]; 
<a name="l00027"></a>00027 <span class="keyword">static</span> uint8_t m_num_of_currently_pressed_keys; 
<a name="l00028"></a>00028 <span class="keyword">static</span> uint8_t m_number_of_transmitted_keys; 
<a name="l00029"></a>00029 
<a name="l00030"></a>00030 <span class="keyword">static</span> uint8_t m_key_packet[<a class="code" href="group__nrf__drivers__cherry8x16.html#ga61bbda0c3b4c6126d2dd65a0b2cdc3ea" title="Total size of the key packet in bytes.">KEY_PACKET_SIZE</a>]; 
<a name="l00031"></a>00031 
<a name="l00032"></a>00032 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t <span class="keyword">volatile</span> * m_row_port; 
<a name="l00033"></a>00033 <span class="keyword">static</span> uint16_t <span class="keyword">volatile</span> * m_column_port; 
<a name="l00034"></a>00034 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t * matrix_lookup; 
<a name="l00035"></a>00035 
<a name="l00037"></a>00037 <span class="keyword">static</span> <span class="keyword">const</span> uint8_t default_matrix_lookup[CHERRY8x16_NUM_OF_COLUMNS*CHERRY8x16_NUM_OF_ROWS] =
<a name="l00038"></a>00038 {
<a name="l00039"></a>00039   0xE7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00040"></a>00040   0xE3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00041"></a>00041   0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00042"></a>00042   0xE0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00043"></a>00043   0xE1, 0xE6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00044"></a>00044   0xE2, 0xE5, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
<a name="l00045"></a>00045   0x29, 0x3E, 0x3D, 0x3C, 0x3B, 0x3A, 0x3F, 0x40,
<a name="l00046"></a>00046   0x1E, 0x23, 0x22, 0x21, 0x20, 0x1F, 0x24, 0x25,
<a name="l00047"></a>00047   0x4F, 0x43, 0x47, 0x53, 0x46, 0x48, 0x42, 0x41,
<a name="l00048"></a>00048   0x51, 0x2D, 0x2E, 0x2A, 0x00, 0x4A, 0x27, 0x26,
<a name="l00049"></a>00049   0x52, 0x13, 0x2F, 0x30, 0x00, 0x4B, 0x12, 0x0C,
<a name="l00050"></a>00050   0x50, 0x33, 0x34, 0x32, 0x28, 0x4E, 0x0F, 0x0E,
<a name="l00051"></a>00051   0x2C, 0x38, 0x4C, 0x49, 0x65, 0x4D, 0x37, 0x36,
<a name="l00052"></a>00052   0x35, 0x05, 0x19, 0x06, 0x1B, 0x1D, 0x11, 0x10,
<a name="l00053"></a>00053   0x39, 0x0A, 0x09, 0x07, 0x16, 0x04, 0x0B, 0x0D,
<a name="l00054"></a>00054   0x2B, 0x17, 0x15, 0x08, 0x1A, 0x14, 0x1C, 0x18
<a name="l00055"></a>00055 };
<a name="l00056"></a>00056 
<a name="l00057"></a>00057 <span class="keyword">static</span> <span class="keywordtype">bool</span> cherry8x16_have_keys_changed(<span class="keyword">const</span> uint8_t *state_now, uint8_t number_of_now_pressed_keys, <span class="keyword">const</span> uint8_t *state_before, uint8_t number_of_before_pressed_keys);
<a name="l00058"></a>00058 <span class="keyword">static</span> <span class="keywordtype">bool</span> cherry8x16_keymatrix_read(uint8_t *pressed_keys, uint8_t *number_of_pressed_keys);
<a name="l00059"></a>00059 <span class="keyword">static</span> <span class="keywordtype">void</span> cherry8x16_keypacket_addkey(uint8_t key);
<a name="l00060"></a>00060 <span class="keyword">static</span> <span class="keywordtype">void</span> cherry8x16_keypacket_create(uint8_t *key_packet, uint8_t key_packet_size);
<a name="l00061"></a>00061 <span class="keyword">static</span> <span class="keywordtype">void</span> cherry8x16_remap_fn_keys(uint8_t *keys, uint8_t number_of_keys);
<a name="l00062"></a>00062 <span class="keyword">static</span> uint8_t cherry8x16_row_read(<span class="keywordtype">void</span>);
<a name="l00063"></a>00063 
<a name="l00064"></a><a class="code" href="group__nrf__drivers__cherry8x16.html#ga4d88841d6f21173bb304d2bd41f67c91">00064</a> <a class="code" href="group__nrf__drivers__cherry8x16.html#gabb88a2cc8f85ba70003398a2e9e26a5e">cherry8x16_status_t</a> <a class="code" href="group__nrf__drivers__cherry8x16.html#ga4d88841d6f21173bb304d2bd41f67c91">cherry8x16_init</a>(<span class="keyword">const</span> uint8_t <span class="keyword">volatile</span> * row_port, uint16_t * column_port, <span class="keyword">const</span> uint8_t * key_lookup_matrix)
<a name="l00065"></a>00065 {
<a name="l00066"></a>00066   <a class="code" href="group__nrf__drivers__cherry8x16.html#gabb88a2cc8f85ba70003398a2e9e26a5e">cherry8x16_status_t</a> status = <a class="code" href="group__nrf__drivers__cherry8x16.html#ggabb88a2cc8f85ba70003398a2e9e26a5ea7c38ac713571ced5641f27dc14091f8d">CHERRY8x16_OK</a>;
<a name="l00067"></a>00067 
<a name="l00068"></a>00068   <span class="keywordflow">if</span> (row_port == 0 || column_port == 0)
<a name="l00069"></a>00069   {
<a name="l00070"></a>00070     status = <a class="code" href="group__nrf__drivers__cherry8x16.html#ggabb88a2cc8f85ba70003398a2e9e26a5ea5d6d998931af38e703f9569f89858fcc">CHERRY8x16_INVALID_PARAMETER</a>;
<a name="l00071"></a>00071   }
<a name="l00072"></a>00072   <span class="keywordflow">else</span>
<a name="l00073"></a>00073   {
<a name="l00074"></a>00074     m_row_port = row_port;
<a name="l00075"></a>00075     m_column_port = column_port;
<a name="l00076"></a>00076 
<a name="l00077"></a>00077     *m_column_port = 0x0000;
<a name="l00078"></a>00078     <span class="keywordflow">if</span> (*m_row_port != 0x00)
<a name="l00079"></a>00079     {
<a name="l00080"></a>00080       status = <a class="code" href="group__nrf__drivers__cherry8x16.html#ggabb88a2cc8f85ba70003398a2e9e26a5ea163e64073d78aae09ebdb9c5edcc634b">CHERRY8x16_NOT_DETECTED</a>;
<a name="l00081"></a>00081     }
<a name="l00082"></a>00082     <span class="keywordflow">else</span>
<a name="l00083"></a>00083     {
<a name="l00084"></a>00084       m_num_of_currently_pressed_keys = 0;
<a name="l00085"></a>00085       m_number_of_transmitted_keys = 0;
<a name="l00086"></a>00086 
<a name="l00087"></a>00087       <span class="keywordflow">for</span> (uint_fast8_t i = <a class="code" href="group__nrf__drivers__cherry8x16.html#ga96705cbd765dd973315e95ac650c68e8" title="Maximum number of pressed keys kept in buffers.">CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS</a>; i--;)
<a name="l00088"></a>00088       {
<a name="l00089"></a>00089         m_currently_pressed_keys[i] = 0;
<a name="l00090"></a>00090         m_transmitted_keys[i] = 0;
<a name="l00091"></a>00091       }
<a name="l00092"></a>00092     }
<a name="l00093"></a>00093 
<a name="l00094"></a>00094     <span class="keywordflow">if</span> (key_lookup_matrix == <a class="code" href="group__nrf__drivers__cherry8x16.html#gacb3e11a73fa9f827d1cb395d7474886b" title="If passed to cherry8x16_init, default lookup matrix will be used.">CHERRY8x16_DEFAULT_KEY_LOOKUP_MATRIX</a>)
<a name="l00095"></a>00095     {
<a name="l00096"></a>00096       matrix_lookup = default_matrix_lookup;
<a name="l00097"></a>00097     }
<a name="l00098"></a>00098     <span class="keywordflow">else</span>
<a name="l00099"></a>00099     {
<a name="l00100"></a>00100       matrix_lookup = key_lookup_matrix;
<a name="l00101"></a>00101     }
<a name="l00102"></a>00102   }
<a name="l00103"></a>00103 
<a name="l00104"></a>00104   <span class="keywordflow">return</span> status;
<a name="l00105"></a>00105 }
<a name="l00106"></a>00106 
<a name="l00107"></a><a class="code" href="group__nrf__drivers__cherry8x16.html#ga0079fdc53514fc2c48f98779f0c24719">00107</a> <span class="keywordtype">bool</span> <a class="code" href="group__nrf__drivers__cherry8x16.html#ga0079fdc53514fc2c48f98779f0c24719">cherry8x16_new_packet</a>(<span class="keyword">const</span> uint8_t ** p_key_packet, uint8_t *p_key_packet_size)
<a name="l00108"></a>00108 {
<a name="l00109"></a>00109   <span class="keywordtype">bool</span> new_packet_prepared;
<a name="l00110"></a>00110 
<a name="l00111"></a>00111   <span class="comment">// Save currently pressed keys</span>
<a name="l00112"></a>00112   <span class="keywordflow">for</span> (uint_fast8_t i = <a class="code" href="group__nrf__drivers__cherry8x16.html#ga96705cbd765dd973315e95ac650c68e8" title="Maximum number of pressed keys kept in buffers.">CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS</a>; i--;)
<a name="l00113"></a>00113   {
<a name="l00114"></a>00114     m_transmitted_keys[i] = m_currently_pressed_keys[i];
<a name="l00115"></a>00115   }
<a name="l00116"></a>00116   m_number_of_transmitted_keys = m_num_of_currently_pressed_keys;
<a name="l00117"></a>00117 
<a name="l00118"></a>00118   <span class="comment">// Create a new packet if key states have changed and there are no keys blocking each other (ghosting/phantom keys)</span>
<a name="l00119"></a>00119   <span class="keywordflow">if</span> (cherry8x16_keymatrix_read(m_currently_pressed_keys, &amp;m_num_of_currently_pressed_keys))
<a name="l00120"></a>00120   {
<a name="l00121"></a>00121     <span class="keywordflow">if</span> (cherry8x16_have_keys_changed(m_currently_pressed_keys, m_num_of_currently_pressed_keys, m_transmitted_keys, m_number_of_transmitted_keys))
<a name="l00122"></a>00122     {
<a name="l00123"></a>00123       cherry8x16_keypacket_create(&amp;m_key_packet[0], <a class="code" href="group__nrf__drivers__cherry8x16.html#ga61bbda0c3b4c6126d2dd65a0b2cdc3ea" title="Total size of the key packet in bytes.">KEY_PACKET_SIZE</a>);
<a name="l00124"></a>00124       *p_key_packet = &amp;m_key_packet[0];
<a name="l00125"></a>00125       *p_key_packet_size = <a class="code" href="group__nrf__drivers__cherry8x16.html#ga61bbda0c3b4c6126d2dd65a0b2cdc3ea" title="Total size of the key packet in bytes.">KEY_PACKET_SIZE</a>;
<a name="l00126"></a>00126       new_packet_prepared = <span class="keyword">true</span>;
<a name="l00127"></a>00127     }
<a name="l00128"></a>00128     <span class="keywordflow">else</span>
<a name="l00129"></a>00129     {
<a name="l00130"></a>00130       <span class="comment">// The same keys are still pressed, no need to create a new packet</span>
<a name="l00131"></a>00131       new_packet_prepared = <span class="keyword">false</span>;
<a name="l00132"></a>00132     }
<a name="l00133"></a>00133   }
<a name="l00134"></a>00134   <span class="keywordflow">else</span>
<a name="l00135"></a>00135   {
<a name="l00136"></a>00136     <span class="comment">// Ghosting detected. Don&#39;t create a packet.</span>
<a name="l00137"></a>00137     new_packet_prepared = <span class="keyword">false</span>;
<a name="l00138"></a>00138   }
<a name="l00139"></a>00139 
<a name="l00140"></a>00140   <span class="keywordflow">return</span> new_packet_prepared;
<a name="l00141"></a>00141 }
<a name="l00142"></a>00142 
<a name="l00143"></a>00143 
<a name="l00149"></a>00149 <span class="keyword">static</span> uint8_t cherry8x16_row_read(<span class="keywordtype">void</span>)
<a name="l00150"></a>00150 {
<a name="l00151"></a>00151   <span class="keywordflow">return</span> *m_row_port;
<a name="l00152"></a>00152 }
<a name="l00153"></a>00153 
<a name="l00165"></a>00165 <span class="keyword">static</span> <span class="keywordtype">bool</span> cherry8x16_keymatrix_read(uint8_t *pressed_keys, uint8_t *number_of_pressed_keys)
<a name="l00166"></a>00166 {
<a name="l00167"></a>00167   uint_fast8_t row_state[CHERRY8x16_NUM_OF_COLUMNS];
<a name="l00168"></a>00168   uint_fast8_t blocking_mask = 0;
<a name="l00169"></a>00169 
<a name="l00170"></a>00170   *number_of_pressed_keys = 0;
<a name="l00171"></a>00171 
<a name="l00172"></a>00172   <span class="keywordflow">for</span> (uint_fast8_t column = CHERRY8x16_NUM_OF_COLUMNS; column--;)
<a name="l00173"></a>00173   {
<a name="l00174"></a>00174     <span class="comment">// drive column under test</span>
<a name="l00175"></a>00175     *m_column_port = (uint16_t)(1UL &lt;&lt; column);
<a name="l00176"></a>00176     row_state[column] = cherry8x16_row_read();
<a name="l00177"></a>00177 
<a name="l00178"></a>00178     <span class="comment">// Check if any keys are pressed</span>
<a name="l00179"></a>00179     <span class="keywordflow">if</span> (row_state[column] != 0)
<a name="l00180"></a>00180     {
<a name="l00181"></a>00181       uint_fast8_t detected_keypresses_on_column = 0;
<a name="l00182"></a>00182 
<a name="l00183"></a>00183       <span class="comment">// Loop through rows, check for active rows and add pressed keys to the array</span>
<a name="l00184"></a>00184       <span class="keywordflow">for</span> (uint_fast8_t row = CHERRY8x16_NUM_OF_ROWS; row--;)
<a name="l00185"></a>00185       {
<a name="l00186"></a>00186         <span class="keywordflow">if</span> (row_state[column] &amp; (1U &lt;&lt; row))
<a name="l00187"></a>00187         {
<a name="l00188"></a>00188           <span class="keywordflow">if</span> (*number_of_pressed_keys &lt; <a class="code" href="group__nrf__drivers__cherry8x16.html#ga96705cbd765dd973315e95ac650c68e8" title="Maximum number of pressed keys kept in buffers.">CHERRY8x16_MAX_NUM_OF_PRESSED_KEYS</a>)
<a name="l00189"></a>00189           {
<a name="l00190"></a>00190             *pressed_keys = matrix_lookup[column*CHERRY8x16_NUM_OF_ROWS + row];
<a name="l00191"></a>00191             pressed_keys++;
<a name="l00192"></a>00192             (*number_of_pressed_keys)++;
<a name="l00193"></a>00193           }
<a name="l00194"></a>00194           detected_keypresses_on_column++;
<a name="l00195"></a>00195         }
<a name="l00196"></a>00196       }
<a name="l00197"></a>00197 
<a name="l00198"></a>00198       <span class="keywordflow">if</span> (detected_keypresses_on_column &gt; 1)
<a name="l00199"></a>00199       {
<a name="l00200"></a>00200         <span class="keywordflow">if</span> (blocking_mask &amp; row_state[column])
<a name="l00201"></a>00201         {
<a name="l00202"></a>00202           <span class="comment">// Cannot determine reliably all pressed keys, two or more keys are blocking each other.</span>
<a name="l00203"></a>00203           <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00204"></a>00204         }
<a name="l00205"></a>00205       }
<a name="l00206"></a>00206       blocking_mask |= row_state[column];
<a name="l00207"></a>00207     }
<a name="l00208"></a>00208   }
<a name="l00209"></a>00209 
<a name="l00210"></a>00210   <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00211"></a>00211 }
<a name="l00212"></a>00212 
<a name="l00219"></a>00219 <span class="keyword">static</span> <span class="keywordtype">void</span> cherry8x16_remap_fn_keys(uint8_t *keys, uint8_t number_of_keys)
<a name="l00220"></a>00220 {
<a name="l00221"></a>00221 
<a name="l00222"></a>00222   <span class="comment">/*lint -e845 -save // A zero has been given as right argument to operator &#39;&lt;&lt;&#39;&quot; */</span>
<a name="l00223"></a>00223   <span class="comment">/*lint -e778 -save // Constant expression evaluates to zero */</span>
<a name="l00224"></a>00224 
<a name="l00225"></a>00225 <span class="preprocessor">#define MODIFIER_LEFT_CONTROL_HID  0xE0</span>
<a name="l00226"></a>00226 <span class="preprocessor"></span><span class="preprocessor">#define MODIFER_RIGHT_CONTROL_HID  0xE4</span>
<a name="l00227"></a>00227 <span class="preprocessor"></span>  <span class="comment">// Check if Fn key is pressed along with any other modifier key (only usage now is Fn+Left_Ctrl = Right Ctrl)</span>
<a name="l00228"></a>00228   <span class="comment">// So we modify the modifier byte if Fn+Left_Ctrl is pressed, HID for left_Ctrl = 0xE0</span>
<a name="l00229"></a>00229   <span class="keywordflow">if</span> ( keys[0] &amp; (1UL&lt;&lt;(MODIFIER_LEFT_CONTROL_HID-MODIFIER_HID_START)) )
<a name="l00230"></a>00230   {
<a name="l00231"></a>00231     keys[0] &amp;= ~(1UL&lt;&lt;(MODIFIER_LEFT_CONTROL_HID-MODIFIER_HID_START));
<a name="l00232"></a>00232     keys[0] |= (1UL&lt;&lt;(MODIFER_RIGHT_CONTROL_HID-MODIFIER_HID_START));
<a name="l00233"></a>00233   }
<a name="l00234"></a>00234 
<a name="l00235"></a>00235   <span class="comment">/*lint -restore */</span>
<a name="l00236"></a>00236   <span class="comment">/*lint -restore */</span>
<a name="l00237"></a>00237 
<a name="l00238"></a>00238   <span class="keywordflow">for</span> (uint_fast8_t i = 2; i &lt; number_of_keys; i++)
<a name="l00239"></a>00239   {
<a name="l00240"></a>00240     <span class="keywordflow">switch</span> (keys[i])
<a name="l00241"></a>00241     {
<a name="l00242"></a>00242       <span class="keywordflow">case</span> 0x10: <span class="comment">//&#39;M&#39;</span>
<a name="l00243"></a>00243         keys[i] = 0x62; <span class="comment">//Keypad 0</span>
<a name="l00244"></a>00244         <span class="keywordflow">break</span>;
<a name="l00245"></a>00245       <span class="keywordflow">case</span> 0x37: <span class="comment">//&#39;&gt;&#39;</span>
<a name="l00246"></a>00246         keys[i] = 0x63; <span class="comment">//Keypad .</span>
<a name="l00247"></a>00247         <span class="keywordflow">break</span>;
<a name="l00248"></a>00248       <span class="keywordflow">case</span> 0x38: <span class="comment">//&#39;/&#39;</span>
<a name="l00249"></a>00249         keys[i] = 0x54; <span class="comment">//Keypad /</span>
<a name="l00250"></a>00250         <span class="keywordflow">break</span>;
<a name="l00251"></a>00251       <span class="keywordflow">case</span> 0x0D: <span class="comment">//&#39;J&#39;</span>
<a name="l00252"></a>00252         keys[i] = 0x59; <span class="comment">//Keypad 1</span>
<a name="l00253"></a>00253         <span class="keywordflow">break</span>;
<a name="l00254"></a>00254       <span class="keywordflow">case</span> 0x0E: <span class="comment">//&#39;K&#39;</span>
<a name="l00255"></a>00255         keys[i] = 0x5A; <span class="comment">//Keypad 2</span>
<a name="l00256"></a>00256         <span class="keywordflow">break</span>;
<a name="l00257"></a>00257       <span class="keywordflow">case</span> 0x0F: <span class="comment">//&#39;L&#39;</span>
<a name="l00258"></a>00258         keys[i] = 0x5B; <span class="comment">//Keypad 3</span>
<a name="l00259"></a>00259         <span class="keywordflow">break</span>;
<a name="l00260"></a>00260       <span class="keywordflow">case</span> 0x33: <span class="comment">//&#39;&#39;</span>
<a name="l00261"></a>00261         keys[i] = 0x57; <span class="comment">//Keypad +</span>
<a name="l00262"></a>00262         <span class="keywordflow">break</span>;
<a name="l00263"></a>00263       <span class="keywordflow">case</span> 0x28: <span class="comment">//&#39;Enter&#39;</span>
<a name="l00264"></a>00264         keys[i] = 0x58; <span class="comment">//Keypad enter</span>
<a name="l00265"></a>00265         <span class="keywordflow">break</span>;
<a name="l00266"></a>00266       <span class="keywordflow">case</span> 0x18: <span class="comment">//&#39;U&#39;</span>
<a name="l00267"></a>00267         keys[i] = 0x5C; <span class="comment">//Keypad 4</span>
<a name="l00268"></a>00268         <span class="keywordflow">break</span>;
<a name="l00269"></a>00269       <span class="keywordflow">case</span> 0x0C: <span class="comment">//&#39;I&#39;</span>
<a name="l00270"></a>00270         keys[i] = 0x5D; <span class="comment">//Keypad 5</span>
<a name="l00271"></a>00271         <span class="keywordflow">break</span>;
<a name="l00272"></a>00272       <span class="keywordflow">case</span> 0x12: <span class="comment">//&#39;O&#39;</span>
<a name="l00273"></a>00273         keys[i] = 0x5E; <span class="comment">//Keypad 6</span>
<a name="l00274"></a>00274         <span class="keywordflow">break</span>;
<a name="l00275"></a>00275       <span class="keywordflow">case</span> 0x13: <span class="comment">//&#39;P&#39;</span>
<a name="l00276"></a>00276         keys[i] = 0x56; <span class="comment">//Keypad -</span>
<a name="l00277"></a>00277         <span class="keywordflow">break</span>;
<a name="l00278"></a>00278       <span class="keywordflow">case</span> 0x24: <span class="comment">//&#39;7&#39;</span>
<a name="l00279"></a>00279         keys[i] = 0x5F; <span class="comment">//Keypad 7</span>
<a name="l00280"></a>00280         <span class="keywordflow">break</span>;
<a name="l00281"></a>00281       <span class="keywordflow">case</span> 0x25: <span class="comment">//&#39;8&#39;</span>
<a name="l00282"></a>00282         keys[i] = 0x60; <span class="comment">//Keypad 8</span>
<a name="l00283"></a>00283         <span class="keywordflow">break</span>;
<a name="l00284"></a>00284       <span class="keywordflow">case</span> 0x26: <span class="comment">//&#39;9&#39;</span>
<a name="l00285"></a>00285         keys[i] = 0x61; <span class="comment">//Keypad 9</span>
<a name="l00286"></a>00286         <span class="keywordflow">break</span>;
<a name="l00287"></a>00287       <span class="keywordflow">case</span> 0x27: <span class="comment">//&#39;0&#39;</span>
<a name="l00288"></a>00288         keys[i] = 0x55; <span class="comment">//Keypad *</span>
<a name="l00289"></a>00289         <span class="keywordflow">break</span>;
<a name="l00290"></a>00290       <span class="keywordflow">case</span> 0x3A: <span class="comment">//&#39;F1&#39;</span>
<a name="l00291"></a>00291         keys[i] = 0x44; <span class="comment">//&#39;F11&#39;</span>
<a name="l00292"></a>00292         <span class="keywordflow">break</span>;
<a name="l00293"></a>00293       <span class="keywordflow">case</span> 0x3B: <span class="comment">//&#39;F2&#39;</span>
<a name="l00294"></a>00294         keys[i] = 0x45; <span class="comment">//&#39;F12&#39;</span>
<a name="l00295"></a>00295         <span class="keywordflow">break</span>;
<a name="l00296"></a>00296       <span class="keywordflow">default</span>:
<a name="l00297"></a>00297         <span class="keywordflow">break</span>;
<a name="l00298"></a>00298     }
<a name="l00299"></a>00299   }
<a name="l00300"></a>00300 }
<a name="l00301"></a>00301 
<a name="l00313"></a>00313 <span class="keyword">static</span> <span class="keywordtype">bool</span> cherry8x16_have_keys_changed(<span class="keyword">const</span> uint8_t *state_now, uint8_t number_of_now_pressed_keys, <span class="keyword">const</span> uint8_t *state_before, uint8_t number_of_before_pressed_keys)
<a name="l00314"></a>00314 {
<a name="l00315"></a>00315   <span class="keywordflow">if</span> (number_of_now_pressed_keys != number_of_before_pressed_keys)
<a name="l00316"></a>00316   {
<a name="l00317"></a>00317     <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00318"></a>00318   }
<a name="l00319"></a>00319   <span class="keywordflow">else</span>
<a name="l00320"></a>00320   {
<a name="l00321"></a>00321     <span class="keywordflow">for</span> (uint_fast8_t i = number_of_now_pressed_keys; i--;)
<a name="l00322"></a>00322     {
<a name="l00323"></a>00323       <span class="keywordflow">if</span> (state_now[i] != state_before[i])
<a name="l00324"></a>00324       {
<a name="l00325"></a>00325         <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00326"></a>00326       }
<a name="l00327"></a>00327     }
<a name="l00328"></a>00328   }
<a name="l00329"></a>00329   <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00330"></a>00330 }
<a name="l00331"></a>00331 
<a name="l00340"></a>00340 <span class="keyword">static</span> <span class="keywordtype">void</span> cherry8x16_keypacket_addkey(uint8_t key)
<a name="l00341"></a>00341 {
<a name="l00342"></a>00342   <span class="keywordflow">for</span> (uint_fast8_t i = <a class="code" href="group__nrf__drivers__cherry8x16.html#gaa44734bdd788259bfec3a2d020b88ae5" title="Start index in the key packet where pressed keys are stored.">KEY_PACKET_KEY_INDEX</a>; i &lt; <a class="code" href="group__nrf__drivers__cherry8x16.html#ga61bbda0c3b4c6126d2dd65a0b2cdc3ea" title="Total size of the key packet in bytes.">KEY_PACKET_SIZE</a>; i++)
<a name="l00343"></a>00343   {
<a name="l00344"></a>00344     <span class="keywordflow">if</span> (m_key_packet[i] == key)
<a name="l00345"></a>00345     {
<a name="l00346"></a>00346       <span class="keywordflow">return</span>;
<a name="l00347"></a>00347     }
<a name="l00348"></a>00348   }
<a name="l00349"></a>00349 
<a name="l00350"></a>00350   <span class="keywordflow">for</span> (uint_fast8_t i = <a class="code" href="group__nrf__drivers__cherry8x16.html#gaa44734bdd788259bfec3a2d020b88ae5" title="Start index in the key packet where pressed keys are stored.">KEY_PACKET_KEY_INDEX</a>; i &lt; <a class="code" href="group__nrf__drivers__cherry8x16.html#ga61bbda0c3b4c6126d2dd65a0b2cdc3ea" title="Total size of the key packet in bytes.">KEY_PACKET_SIZE</a>; i++)
<a name="l00351"></a>00351   {
<a name="l00352"></a>00352     <span class="keywordflow">if</span> (m_key_packet[i] == <a class="code" href="group__nrf__drivers__cherry8x16.html#gaf91da4c4186290332d254c5f7aa1215a" title="Value to be stored to key index to indicate no key is pressed.">KEY_PACKET_NO_KEY</a>)
<a name="l00353"></a>00353     {
<a name="l00354"></a>00354       m_key_packet[i] = key;
<a name="l00355"></a>00355       <span class="keywordflow">return</span>;
<a name="l00356"></a>00356     }
<a name="l00357"></a>00357   }
<a name="l00358"></a>00358 }
<a name="l00359"></a>00359 
<a name="l00371"></a>00371 <span class="keyword">static</span> <span class="keywordtype">void</span> cherry8x16_keypacket_create(uint8_t *key_packet, uint8_t key_packet_size)
<a name="l00372"></a>00372 {
<a name="l00373"></a>00373   <span class="comment">// Clear key_packet contents</span>
<a name="l00374"></a>00374   <span class="keywordflow">for</span> (uint_fast8_t i = <a class="code" href="group__nrf__drivers__cherry8x16.html#gaa44734bdd788259bfec3a2d020b88ae5" title="Start index in the key packet where pressed keys are stored.">KEY_PACKET_KEY_INDEX</a>; i &lt; key_packet_size; i++)
<a name="l00375"></a>00375   {
<a name="l00376"></a>00376     key_packet[i] = <a class="code" href="group__nrf__drivers__cherry8x16.html#gaf91da4c4186290332d254c5f7aa1215a" title="Value to be stored to key index to indicate no key is pressed.">KEY_PACKET_NO_KEY</a>;
<a name="l00377"></a>00377   }
<a name="l00378"></a>00378   key_packet[<a class="code" href="group__nrf__drivers__cherry8x16.html#ga41430f808d8d3b096877de667eb4d086" title="Index in the key packet where modifier keys such as ALT and Control are stored.">KEY_PACKET_MODIFIER_KEY_INDEX</a>] = 0;
<a name="l00379"></a>00379   key_packet[<a class="code" href="group__nrf__drivers__cherry8x16.html#ga6c26e14ce8c196c829b01213ebe2a257" title="Index in the key packet where OEMs can store information.">KEY_PACKET_RESERVED_INDEX</a>] = 0;
<a name="l00380"></a>00380 
<a name="l00381"></a>00381   <span class="comment">// Give priority to keys that were already pressed when we transmitted them the last time.</span>
<a name="l00382"></a>00382   <span class="keywordflow">for</span> (uint_fast8_t i = 0 ; i &lt; m_number_of_transmitted_keys; i++)
<a name="l00383"></a>00383   {
<a name="l00384"></a>00384     <span class="keywordflow">for</span> (uint_fast8_t j = 0; j &lt; m_num_of_currently_pressed_keys; j++)
<a name="l00385"></a>00385     {
<a name="l00386"></a>00386       <span class="keywordflow">if</span> (m_transmitted_keys[i] == m_currently_pressed_keys[j])
<a name="l00387"></a>00387       {
<a name="l00388"></a>00388         cherry8x16_keypacket_addkey(m_currently_pressed_keys[j]);
<a name="l00389"></a>00389         <span class="keywordflow">break</span>;
<a name="l00390"></a>00390       }
<a name="l00391"></a>00391     }
<a name="l00392"></a>00392   }
<a name="l00393"></a>00393 
<a name="l00394"></a>00394   <span class="keywordtype">bool</span> fn_key_is_set = <span class="keyword">false</span>;
<a name="l00395"></a>00395 
<a name="l00396"></a>00396   <span class="comment">// Detect if Fn is pressed, detect modifier keys, and add rest of the keys to the packet</span>
<a name="l00397"></a>00397   <span class="keywordflow">for</span> (uint_fast8_t i = 0; i &lt; m_num_of_currently_pressed_keys; i++)
<a name="l00398"></a>00398   {
<a name="l00399"></a>00399     <span class="keywordflow">if</span> (m_currently_pressed_keys[i] == 0xFF) <span class="comment">// Pressing Fn key changes function of certain keys and it must handled by the firmware</span>
<a name="l00400"></a>00400     {
<a name="l00401"></a>00401       fn_key_is_set = <span class="keyword">true</span>;
<a name="l00402"></a>00402     }
<a name="l00403"></a>00403     <span class="comment">// Modifier HID usage codes are from 0xE0 to 0xE7</span>
<a name="l00404"></a>00404     <span class="keywordflow">else</span> <span class="keywordflow">if</span> (m_currently_pressed_keys[i] &gt;= MODIFIER_HID_START &amp;&amp; m_currently_pressed_keys[i] &lt;= MODIFIER_HID_END) <span class="comment">// Detect and set modifier keys</span>
<a name="l00405"></a>00405     {
<a name="l00406"></a>00406       key_packet[<a class="code" href="group__nrf__drivers__cherry8x16.html#ga41430f808d8d3b096877de667eb4d086" title="Index in the key packet where modifier keys such as ALT and Control are stored.">KEY_PACKET_MODIFIER_KEY_INDEX</a>] |= (uint8_t)(1U &lt;&lt; (m_currently_pressed_keys[i] - MODIFIER_HID_START));
<a name="l00407"></a>00407     }
<a name="l00408"></a>00408     <span class="keywordflow">else</span> <span class="keywordflow">if</span> (m_currently_pressed_keys[i] != 0)
<a name="l00409"></a>00409     {
<a name="l00410"></a>00410       cherry8x16_keypacket_addkey(m_currently_pressed_keys[i]);
<a name="l00411"></a>00411     }
<a name="l00412"></a>00412   }
<a name="l00413"></a>00413 
<a name="l00414"></a>00414   <span class="keywordflow">if</span> (fn_key_is_set)
<a name="l00415"></a>00415   {
<a name="l00416"></a>00416     cherry8x16_remap_fn_keys(&amp;key_packet[0], <a class="code" href="group__nrf__drivers__cherry8x16.html#ga3a5a7559365a12f6ea3955768b8b5382" title="Maximum number of keys that can be stored into the key packet.">KEY_PACKET_MAX_KEYS</a>);
<a name="l00417"></a>00417   }
<a name="l00418"></a>00418 }
</pre></div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>
      <li class="navelem"><b>cherry8x16.c</b>      </li>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Data Structures</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>


    <li class="footer">
      Copyright &copy 2006-2011 <a href="http://www.nordicsemi.no" style="text-decoration:none">Nordic Semiconductor</a>.
      All Rights Reserved.
      <a href="disclaimer.html">Disclaimer</a>
    </li>
   </ul>
 </div>


</body>
</html>
